package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Name DishServiceImpl
 * @Author stella
 * @Date 2023-03-05 11:07 AM
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 菜品列表
     * 分页条件查询
     */
    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        //构建分页
        Page<Dish> dishPage = new Page<>(pageNum, pageSize);
        //构建lambdaQueryWrapper查询菜品的条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name), Dish::getName, name);
        //查询分页
        dishPage = dishMapper.selectPage(dishPage, qw);
        //dish的id 去查口味，   catecoryID 查分类
        List<Dish> dishList = dishPage.getRecords();
        for (Dish dish : dishList) {
            // 1-查口味

            LambdaQueryWrapper<DishFlavor> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(DishFlavor::getDishId, dish.getId());
            // 1-1查口味列表
            List<DishFlavor> flavors = dishFlavorMapper.selectList(qw2);
            // 1-2口味封装进dish实体类
            dish.setFlavors(flavors);
            // 2-查询菜品分类
            Category category = categoryMapper.selectById(dish.getCategoryId());
            // 2-2把分类名称封装进dish实体类
            dish.setCategoryName(category.getName());
        }

        return dishPage;
    }

    /**
     * 新增菜品
     * @param dish
     */
    @Override
    public void save(Dish dish) {
        // 1.直接保存菜品基本信息
        log.info("新增之前id：{}", dish.getId());
        dishMapper.insert(dish);
        log.info("新增之后id：{}", dish.getId()); // 主键返回
        //把其他的东西存进其他的表
        List<DishFlavor> flavors = dish.getFlavors();
        for (DishFlavor flavor : flavors) {
            //把这个flavor设个id 直接存进去
            flavor.setDishId(dish.getId());
            //存
            dishFlavorMapper.insert(flavor);
        }
    }

    /**
     * 菜品修改：回显菜品详情
     * @param id
     * @return
     */
    @Override
    public Dish findById(Long id) {
        //根据id查询dish表
        Dish dish = dishMapper.selectById(id);
        // 查dish—flavor表
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavorList = dishFlavorMapper.selectList(qw);
        //将口味集合存入dish类
        dish.setFlavors(flavorList);
        //返回菜品
        return dish;
    }

    /**
     * 修改菜品/口味
     * @param dish
     */
    @Override
    public void update(Dish dish) {
        //更新表1 dish
         dishMapper.updateById(dish);
         //构建修改口味条件
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,dish.getId());
        //根据条件删除旧的口味
        dishFlavorMapper.delete(qw);
        // 3.遍历新口味
        List<DishFlavor> flavorList = dish.getFlavors();
        for (DishFlavor dishFlavor : flavorList) {
            //存
            //关联菜品id
            dishFlavor.setDishId(dish.getId());
            //存进数据库
            dishFlavorMapper.insert(dishFlavor);
        }
    }

    /**
     * 批量启停售
     * @param status
     * @param ids
     */
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        // 根据id更新状态 update dish set status = 0|1 where id in(xxx,xxx,xxx)
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId,ids); // where id in(xxx,xxx,xxx)
        // 如何更新状态  可以用update  改一个status 存进去
        // 构建修改实体
        Dish dish = new Dish();
        dish.setStatus(status); // set status = 0|1
        // 执行数据库
        dishMapper.update(dish, qw);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteBatchId(List<Long> ids) {
        //先判断此菜品是否是停售状态 select count(*) from dish where status = 1 and  id in (xx,xx,xxx)
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId,ids);
        qw.eq(Dish::getStatus,1);
        Integer count = dishMapper.selectCount(qw);
        if (count > 0){
            throw new CustomException("删除的菜品状态必须为已停售！~~");
        }
        // 2.删除菜品  delete from dish where id in (xx,xx,xxx)
        dishMapper.deleteBatchIds(ids);
        // 3.删除口味 delete from dish_flavor where dish_id in (xx,xx,xxx)
        LambdaQueryWrapper<DishFlavor> qw2 = new LambdaQueryWrapper<>();
        qw2.in(DishFlavor::getDishId,ids);
        dishFlavorMapper.delete(qw2);
    }

    /**
     * 回显菜品列表
     * @param categoryId
     * @param name
     * @return
     */
    @Override
    public List<Dish> findList(Long categoryId, String name) {
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(categoryId != null,Dish::getCategoryId,categoryId);
        qw.like(StrUtil.isNotEmpty(name),Dish::getName,name);
        List<Dish> dishes = dishMapper.selectList(qw);
        return dishes;
    }

    /**
     * 根据 分类id 状态查询
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    public List<Dish> findByCategoryId(Long categoryId, Integer status) {
        // 构建条件 select * from dish where category_id = ? and status = ?
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,categoryId);
        qw.eq(Dish::getStatus,status);
        List<Dish> dishList = dishMapper.selectList(qw);
        // 查口味！ select * from dish_flavor where dish_id=?
        dishList.forEach(dish -> {
            LambdaQueryWrapper<DishFlavor> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> flavorList = dishFlavorMapper.selectList(qw2);
            // 封装到dish中
            dish.setFlavors(flavorList);
        });
        return dishList;
    }
}
